package neusoft.example.com.yuejian06.Model;

import neusoft.example.com.yuejian06.Bean.User;
import neusoft.example.com.yuejian06.PublicIp;
import neusoft.example.com.yuejian06.RetrofitListener;
import neusoft.example.com.yuejian06.Service.UserService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;



public class RegisterModel {
    private Retrofit retrofit;
    private UserService service;
    public RegisterModel(){
        retrofit=new Retrofit.Builder()
                .baseUrl(PublicIp.ip+"api/index/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        service=retrofit.create(UserService.class);
    }
    public void register(String user_name, String user_tel, String user_password, String user_email, final RetrofitListener<User> listener){
        Call<User> call=service.register(user_name, user_tel, user_password, user_email);
        call.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                if(response.isSuccessful() && response!=null){
                    listener.onResponse(response.body());
                }else{
                    listener.onFail("服务器炸了！");
                }
            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {
                listener .onFail(t.toString());
            }
        });
    }
}
